﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TireBias.Models;
using WinNemoLib;


namespace TireBias.Controllers
{
    public class NewSampleController : Controller
    {
        //
        // GET: /NewSample/

        public ActionResult Index(string refseq, string empnm, string sTxt)
        {
            ViewBag.Full = "F";
            SessionBag.Current.empnm = empnm;
            SessionBag.Current.Txt = sTxt;
            SessionBag.Current.refId = refseq;
            return View();
        }

        public JsonResult Ins_Sample(List<BIASPLANModel.BIASPLAN_DS> theData)
        {

            BIASPLANModel.BIASPLAN_DS data = theData[0];
                BIASPLANModel mdUpd = new BIASPLANModel();
                string[] seqs = SessionBag.Current.refId.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);

                data._Plandt = seqs[0];
                data._Biasno = SessionBag.Current.biasno;
                data._Shift = SessionBag.Current.shift;
                data._Itemno = seqs[1];
                int itemno = mdUpd.GET_SEQ(data._Plandt, data._Biasno, data._Itemno, true);
                data._Itemno = itemno.ToString();
                data._Sample = "X";
                data._Smpemp = SessionBag.Current.empnm;
                data._Smpshtxt = SessionBag.Current.Txt;
                data._Plants = "L";
                using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
                {
                    cnn.Open();
                    cnn.BeginTransaction();
                    if (mdUpd.Ins_Data(cnn, data, User.Identity.Name))
                    {
                        cnn.CommitTransaction();
                        return Json(new { success = 1, message = "บันทึกเรียบร้อย",subitem =data._Plandt+'-'+ itemno });
                    }
                    else
                    {
                        cnn.RollbackTransaction();
                        return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                    }
                }

        }

        public ActionResult New_Sample(string seq)
        {
            ViewBag.Full = "F";            
            List<BIASPLANModel.BIASPLAN_DS> list = new List<BIASPLANModel.BIASPLAN_DS>();
            BIASPLANModel mdSearch = new BIASPLANModel();
            string[] seqs = seq.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);

            list = mdSearch.GET_TOPCORDPLN_ACT(seqs[0], "1", SessionBag.Current.biasno, "", seqs[1]);
            return View(list[0]);
        }

        public ActionResult RollBack(string seq,string biasno = "", string shift = "")
        {

            BIASPLANModel mdSearch = new BIASPLANModel();
            mdSearch.DEL_NEW_SAMPLE(seq, biasno);
            return RedirectToAction("Header_line","Biashead", new { biasno = biasno, shift = shift });
        }

        public ActionResult New_Sample_SUB(string seq)
        {

            ViewBag.Full = "F";
            List<BIASPLANModel.BIASPLAN_DS> list = new List<BIASPLANModel.BIASPLAN_DS>();
            BIASPLANModel mdSearch = new BIASPLANModel();
            string[] seqs = seq.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);

            list = mdSearch.GET_TOPCORDPLN_ACT(seqs[0], "1", SessionBag.Current.biasno, "", seqs[1]);
            return View(list[0]);
        }


        public JsonResult Run_TireSAMPLE(List<BIASSUBPlanModel.BIASSUBPlan_DS> thedata)
        {
            BIASSUBPlanModel mdSearch = new BIASSUBPlanModel();
            using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();

                if (!mdSearch.UPDATE_SUB_STATUS_BIAS(cnn, thedata[0],"SM",true))
                {
                   cnn.RollbackTransaction();
                    return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                }
              
                if (!mdSearch.UPDATE_LEN_BIAS(cnn, thedata[0]))
                {
                   cnn.RollbackTransaction();
                    return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                }
                
                cnn.CommitTransaction();
                    return Json(new { success = 1, message = "" });
            }

        }


        public ActionResult New_Sample_PROC(string seq)
        {

            ViewBag.Full = "F";
            List<BIASPLANModel.BIASPLAN_DS> list = new List<BIASPLANModel.BIASPLAN_DS>();
            BIASPLANModel mdSearch = new BIASPLANModel();
            string[] seqs = seq.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);

            list = mdSearch.GET_TOPCORDPLN_ACT(seqs[0], "1", SessionBag.Current.biasno, "", seqs[1]);
            return View(list[0]);
        }

        public JsonResult FINIS_TireSTD(List<BIASSUBPlanModel.BIASSUBPlan_DS> thedata)
        {
            BIASSUBPlanModel mdSearch = new BIASSUBPlanModel();
            using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();

                //if (!mdSearch.UPDATE_SUB_STATUS_BIAS(cnn, thedata[0], "F"))
                //{                 

                //    cnn.RollbackTransaction();
                //    return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                //}
                
                
                BIASPLANModel md =new BIASPLANModel();
                if (!md.UPDATE_FIN_SAMP(cnn,User.Identity.Name.ToString(),thedata[0]._plandt + "-" + thedata[0]._itemno,"",thedata[0]._biasno))
                {
                    cnn.RollbackTransaction();
                    return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                }

                cnn.CommitTransaction();
                return Json(new { success = 1, message = "" });
            }

        }

    }
}
